import 'package:hrf_app/common/models/user/user_info.dart';
import 'package:hrf_app/common/services/user.dart';
import 'package:flutter/material.dart';

extension ExVisibility<E> on Widget {
  /// 权限 hasPerm  字符串
  Widget hasPerm({
    Key? key,
    required String? hasPerm,
  }) {
    bool visible = false;
    if (hasPerm != null && hasPerm != "") {
      UserInfo userInfo = UserService.to.profile;
      List? roleList = userInfo.roles;
      if (roleList != null) {
        visible = roleList.contains(hasPerm);
      }
    }

    return Visibility(
      key: key,
      visible: visible,
      child: this,
    );
  }

  /// 权限和visible 同时满足  hasPerm  字符串 ，visible  trun 或者false
  Widget hasPermAndVisible({
    Key? key,
    required String? hasPerm,
    required bool visible,
  }) {
    bool visiblePerm = true;
    if (hasPerm != null && hasPerm != "") {
      UserInfo userInfo = UserService.to.profile;
      List? roleList = userInfo.roles;
      if (roleList != null) {
        visiblePerm = roleList.contains(hasPerm);
      }
    }

    return Visibility(
      key: key,
      visible: visible && visiblePerm,
      child: this,
    );
  }

  ///  正常的是否展示
  Widget toVisibility({
    Key? key,
    required bool visible,
  }) {
    return Visibility(
      key: key,
      visible: visible,
      child: this,
    );
  }
}
